package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kuangxiaoyang.foodapp.bean.FindOneBean;
import com.kuangxiaoyang.foodapp.net.NetUtil;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class FoodDetailActivity extends AppCompatActivity {
    private static final String TAG = "FoodDetailActivity";
    int id = 0;
    TextView name, desc, price;
    ImageView pic;
    Button buy, addcomments, listcomments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        id = getIntent().getIntExtra("foodId", 0);
        initView();
        initData();
        initEvent();
    }


    private void initView() {
        name = findViewById(R.id.tv_food_name);
        desc = findViewById(R.id.tv_food_desc);
        price = findViewById(R.id.tv_food_price);
        pic = findViewById(R.id.iv_food_pic);
        buy = findViewById(R.id.btn_buy);
        listcomments = findViewById(R.id.btn_list_commets);
        addcomments = findViewById(R.id.btn_add_commets);
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id",id);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String url = "http://10.2.64.75:8080/foodserver/food/findone";
                Gson gson = new Gson();
                String data = NetUtil.getData(jsonObject,url);
                final FindOneBean findOneBean = gson.fromJson(data, FindOneBean.class);
                Log.e(TAG,"run:" +findOneBean);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    name.setText(findOneBean.getData().getName());
                    desc.setText(findOneBean.getData().getDescription());
                    price.setText(findOneBean.getData().getPrice()+"元");

                        Picasso.with(FoodDetailActivity.this)
                                .load("http://10.2.64.75:8080/foodserver/"+findOneBean.getData().getIcon())
                                .into(pic);
                    }
                });
            }
        }).start();
    }

    private void initEvent() {
        listcomments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailActivity.this, CommentsListActivity.class);
                startActivity(intent);
            }
        });
        addcomments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailActivity.this, CommentsAddActivity.class);
                startActivity(intent);
            }
        });
    }

}